package com.nur.noteishon;

import java.util.ArrayList;

import com.nur.dto.Nota;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.TextView;

public class NotaAdapter extends ArrayAdapter<Nota> {

	Context context;
	int layoutResourceId;
	ArrayList<Nota> data = null;

	public NotaAdapter(Context context, int layoutResourceId,
			ArrayList<Nota> notas_Data) {
		super(context, layoutResourceId, notas_Data);
		this.layoutResourceId = layoutResourceId;
		this.context = context;
		this.data = notas_Data;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View row = convertView;
		NotaHolder holder = null;

		if (row == null) {
			LayoutInflater inflater = ((Activity) context).getLayoutInflater();
			row = inflater.inflate(layoutResourceId, parent, false);

			holder = new NotaHolder();
			holder.txtTitle = (TextView) row.findViewById(R.id.txtTitle);
			holder.txtFechaInicio = (TextView) row
					.findViewById(R.id.txtFechaInicio);
			holder.txtFechaFin = (TextView) row.findViewById(R.id.txtFechaFin);
			holder.chkItem = (CheckBox) row.findViewById(R.id.chkItem);
			row.setTag(holder);
		} else {
			holder = (NotaHolder) row.getTag();
		}

		com.nur.dto.Nota note = data.get(position);
		holder.txtTitle.setText(note.getComentario());
		holder.txtFechaInicio.setText(note.getHoraInicio());
		holder.txtFechaFin.setText(note.getHoraFin());

		String[] resource_Array = context.getResources().getStringArray(
				R.array.arregloTiposNotas);
		if (note.getTipo().equals(resource_Array[0])) {
			row.setBackgroundColor(Color.rgb(238, 133, 137));// Rojo
		} else if (note.getTipo().equals(resource_Array[1])) {
			row.setBackgroundColor(Color.rgb(224, 226, 247));// Azul
		} else if (note.getTipo().equals(resource_Array[2])) {
			row.setBackgroundColor(Color.rgb(226, 248, 227));// Verde
		} else if (note.getTipo().equals(resource_Array[3])) {
			row.setBackgroundColor(Color.rgb(243, 243, 149));// Amarillo
		} else if (note.getTipo().equals(resource_Array[4])) {
			row.setBackgroundColor(Color.rgb(248, 232, 209));// Naranja
		}
		return row;
	}

	static class NotaHolder {
		TextView txtTitle;
		TextView txtFechaInicio;
		TextView txtFechaFin;
		CheckBox chkItem;
	}
}